<?php

/**
 * 首页
 * david.yan (david.yan@qq.com) by 2013
 * 北京泰和盈动科技有公司 版权所有
 */
class cms extends actionAbstract {

    public $title = '';
    public $css = array();
    public $modules = array();
    public $menu = array();
    public $pos = 2;
	public $type=0;
	public $leftpos=0;
    
	/**
	* 递归要添加的菜单PIN
	 *
	 * @var array
	 */
	protected $add_auth = array ();
	
    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
          $this->info = acl::checkLogin('admininfo',$this->url('common/login'));
        //     if(!isset($_SESSION['admininfo'])){ header('location:'.$this->url('common/login'));}
		$this->menu = $this->loadConfig(); 
		$this->type = isset($_GET['type'])?(int)$_GET['type']:0;
    }
    
    //模型管理
    public function model(){
		$this->leftpos=0;
        $this->loadModel('cms','model');
        $re=$this->cms->modelModel->fetchAll("select * from cms_model");
        
        include $this->loadWidget('masterTheme');
    }
    //添加、修改模型
    public function edit(){
		$this->leftpos=0;
        $this->loadModel('cms','model');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $re=$this->cms->modelModel->fetchRow("select * from cms_model where id=$id");
        }
        if($_POST){
            $id=isset($_POST['id'])?$_POST['id']:0;
            $data=isset($_POST['data'])?$_POST['data']:'';
            if($id){
                $re=$this->cms->modelModel->update($data,$id);
            }else{
                $re=$this->cms->modelModel->insert($data);
            }
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    //修改状态
    public function status(){
        $this->loadModel('cms','model');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->cms->modelModel->fetchRow("select * from cms_model where id=$id");
            if($result&&$result['status']==1){
                $data['status']=2;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->cms->modelModel->update($data,$id);
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
    }
    //删除cms模型
    public function del(){
        $this->loadModel('cms','model');
        $this->loadHelper('extend');
        $id=isset($_GET['id'])?$_GET['id']:0;
        $re=$this->cms->modelModel->delete($id);
        if($re){
            ajaxReturn ('back','操作成功！', 1 );exit;
        }else{
            ajaxReturn ('','操作失败！', 0 );exit;
        }
    }
    
    
    //cms文章管理
    public function info(){
		$this->leftpos=1;
        $this->loadModel('cms','info');
        $this->loadModel('cms','model');
        $result=$this->cms->modelModel->fetchRow("select * from cms_model");
        if(!$result){
            header("location:/index.php/sysadmin/cms/edit");
        }else{
            $re=$this->cms->infoModel->fetchAll("select a.*,b.title as tdname from cms_info as a left join cms_model as b on a.modelid=b.id where b.status=1");
        }
        
        include $this->loadWidget('masterTheme');
    }
    //修改、添加文章
    public function infoEdit(){
		$this->leftpos=1;
        $this->loadModel('cms','info');
        $this->loadModel('cms','model');
        $this->loadHelper('extend');
        $model=$this->cms->modelModel->fetchAll("select * from cms_model where status=1");
        
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->cms->infoModel->fetchRow("select * from cms_info where id=$id");
            
        }
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $id=isset($_POST['id'])?$_POST['id']:0;
            $data['contentTxet']=isset($_POST['contentTxet'])?htmlspecialchars($_POST['contentTxet']):'';
            $data['created']=time();
            $this->loadHelper('uploader');
            $uploader = new uploader();
            if(!empty($_FILES['imgurl']['name'])){
                $data['imgurl']=$uploader->start('imgurl');
            }
            //echo $data['imgurl'];exit;
            if(!empty($_FILES['imgicon']['name'])){
                $data['imgicon']=$uploader->start('imgicon');
            }
            if(!empty($_FILES['texturl']['name'])){
                $data['texturl']=$uploader->start('texturl');
            }
            //echo "<pre>";print_r($data);exit;
            if($id){
                $re=$this->cms->infoModel->update($data,$id);
            }else{
                $re=$this->cms->infoModel->insert($data);
            }
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    //修改文章状态
    public function infoStatus(){
        $this->loadModel('cms','info');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->cms->infoModel->fetchRow("select * from cms_info where id=$id");
            if($result&&$result['status']==1){
                $data['status']=2;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->cms->infoModel->update($data,$id);
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
    }
    //删除文章
    public function infoDel(){
        $this->loadModel('cms','info');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $re=$this->cms->infoModel->delete($id);
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
    }
}